package com.sqisland.dash;

import java.util.Set;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class BluetoothDevicesActivity extends BaseActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bluetooth_devices);

    ListView listView = (ListView) findViewById(R.id.bluetooth_device_list);

    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();

    listView.addHeaderView(createHeader());
    DeviceAdapter adapter = new DeviceAdapter(this,
        pairedDevices.toArray(new BluetoothDevice[] {}));
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(deviceClickListener);

    String type = getIntent().getStringExtra(Constants.KEY_TYPE);
    if (type == null) {
      Toast.makeText(this, R.string.missing_type, Toast.LENGTH_LONG).show();
      finish();
    }
  }

  private OnItemClickListener deviceClickListener = new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
      String type = getIntent().getStringExtra(Constants.KEY_TYPE);

      if (position == 0) {
        clearSensor(type);
        return;
      }

      BluetoothDevice device = (BluetoothDevice) parent.getAdapter()
          .getItem(position);
      setSensor(device.getAddress(), device.getName(), type);
    }
  };

  private void setSensor(String address, String name, String type) {
    String row = TextUtils.join("\t", new String[] { address, name });

    SharedPreferences pref = PreferenceManager
        .getDefaultSharedPreferences(this);
    String key = type + Constants.KEY_SENSOR_SUFFIX;

    SharedPreferences.Editor editor = pref.edit();
    editor.putString(key, row);
    editor.commit();

    sensorsService.updateSensor(type);

    finish();
  }

  private void clearSensor(String type) {
    SharedPreferences pref = PreferenceManager
        .getDefaultSharedPreferences(this);
    String key = type + Constants.KEY_SENSOR_SUFFIX;

    SharedPreferences.Editor editor = pref.edit();
    editor.remove(key);
    editor.commit();

    sensorsService.updateSensor(type);

    finish();
  }

  private View createHeader() {
    View view = LayoutInflater.from(this).inflate(
        android.R.layout.simple_list_item_2, null);
    TextView title = (TextView) view.findViewById(android.R.id.text1);
    title.setText(R.string.none);
    return view;
  }

  private class DeviceAdapter extends ArrayAdapter<BluetoothDevice> {
    private LayoutInflater inflater;

    public DeviceAdapter(Context context, BluetoothDevice[] devices) {
      super(context, android.R.layout.simple_list_item_2, devices);
      inflater = LayoutInflater.from(context);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
      View view = (convertView == null) ? inflater.inflate(
          android.R.layout.simple_list_item_2, null) : convertView;

      ViewHolder holder = (ViewHolder) view.getTag();
      if (holder == null) {
        holder = new ViewHolder(view);
        view.setTag(holder);
      }

      BluetoothDevice device = getItem(position);
      holder.title.setText(device.getName());
      holder.subtitle.setText(device.getAddress());

      return view;
    }

    private class ViewHolder {
      public TextView title;
      public TextView subtitle;

      public ViewHolder(View parent) {
        title = (TextView) parent.findViewById(android.R.id.text1);
        subtitle = (TextView) parent.findViewById(android.R.id.text2);
      }
    }
  }
}